package com.example.lottery_system.controller;

import com.example.lottery_system.common.errcode.ControllerErrorCodeConstant;
import com.example.lottery_system.common.exception.ControllerException;
import com.example.lottery_system.common.utils.JacksonUtil;
import com.example.lottery_system.controller.param.ActivityParam;
import com.example.lottery_system.controller.param.PageParam;
import com.example.lottery_system.controller.result.*;
import com.example.lottery_system.service.ActivityService;
import com.example.lottery_system.service.dto.ActivityDetailDTO;
import com.example.lottery_system.service.dto.ActivityDto;
import com.example.lottery_system.service.dto.ActivityListDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ActivityController {
    Logger logger = LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    ActivityService activityService;
    @RequestMapping("/activity/create")
    public Long createActivity(@Validated @RequestBody ActivityParam param) {
        logger.info("createActivity：：ActivityParam：" + JacksonUtil.writeValueAsString(param));



        ActivityDto activityDto  = activityService.createActivity(param);

        if (activityDto == null) {
            logger.error("createActivity控制层活动创建异常!");
            throw new ControllerException(ControllerErrorCodeConstant.ACTIVITY_CREATE_ERROR);
        }
        return activityDto.getId();
    }

    @RequestMapping("/activity/find-list")
    public PageResult<List<ActivityListResult>> findActivityList(PageParam param) {
        logger.info("findActivityList::PageParam: " + JacksonUtil.writeValueAsString(param));
        PageResult<List<ActivityListDto>> activityDtoActivityList =  activityService.findActivityList(param);

        return conversion(activityDtoActivityList);
    }



    @RequestMapping("/activity-detail/find")
    public ActivityDetailResult findActivityDetail(Long activityId) {
        logger.info("findActivityDetail：：ActivityId：{}",activityId);
        ActivityDetailDTO activityDetailDTO = activityService.findActivityDetailFromRedis(activityId);

        return converstToActDetailResult(activityDetailDTO);
    }


    @RequestMapping("/activity/delete")
    public Boolean deleteActivity(Long activityId) {
        logger.info("deleteActivity::activityId:{}",activityId);
        return activityService.deleteActivity(activityId);
    }

    private ActivityDetailResult converstToActDetailResult(ActivityDetailDTO activityDetailDTO) {
        if (null == activityDetailDTO) {
            throw new ControllerException(ControllerErrorCodeConstant.ACTIVITY_IS_EMPTY);
        }

        List<AUDetailResult> auDetailResultList = activityDetailDTO
                .getUserDtoList()
                .stream()
                .map(userDto -> new AUDetailResult(
                        userDto.getUserId(),
                        userDto.getUserName(),
                        userDto.valid()
                )).collect(Collectors.toList());


        List<APDetailResult> apDetailResultList = activityDetailDTO
                .getPrizeDtoList()
                .stream()
                .map(prizeDto -> new APDetailResult(
                        prizeDto.getId(),
                        prizeDto.getName(),
                        prizeDto.getDesc(),
                        prizeDto.getPrice(),
                        prizeDto.getImageUrl(),
                        prizeDto.getAmount(),
                        prizeDto.getPrizeTiers().getMsg(),
                        prizeDto.valid()
                ))
                .collect(Collectors.toList());

        // 整合
        ActivityDetailResult activityDetailResult = new ActivityDetailResult();
        activityDetailResult.setActivityId(activityDetailDTO.getActivityId());
        activityDetailResult.setActivityName(activityDetailResult.getActivityName());
        activityDetailResult.setDescription(activityDetailResult.getDescription());
        activityDetailResult.setValid(activityDetailDTO.valid());
        activityDetailResult.setUsers(auDetailResultList);
        activityDetailResult.setPrizes(apDetailResultList);
        return activityDetailResult;

    }


    private PageResult<List<ActivityListResult>> conversion(PageResult<List<ActivityListDto>> activityDtoActivityList) {
        if(activityDtoActivityList == null) {
            throw new ControllerException(ControllerErrorCodeConstant.ACTIVITY_FIND_LIST_ERROR);
        }

//            转化
        List<ActivityListResult> activityListResults = activityDtoActivityList.getRecords().stream().map(
                activityListDto -> new ActivityListResult(
                        activityListDto.getActivityId(),
                        activityListDto.getActivityName(),
                        activityListDto.getDescription(),
                        activityListDto.valid()
                )
        ).collect(Collectors.toList());


        PageResult<List<ActivityListResult>> pageResult = new PageResult<>();
        pageResult.setTotal(activityDtoActivityList.getTotal());
        pageResult.setRecords(activityListResults);

        return pageResult;
    }


}

